package com.qili.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qili.entity.supervisor.dto.SupervisorProjectDTO;
import com.qili.service.ActAssigneeService;
import com.qili.service.SysUserService;
import com.qili.service.WorkflowService;
import com.qili.service.sys.RoleService;
import com.qili.service.sys.RoleUserService;
import com.qili.util.JsonUtil;
import org.activiti.engine.IdentityService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
* @Author:zhaoyongke
* @Description:
* @Date:13:44 2020/12/7
*/
@Controller
@RequestMapping("/admin/workflow")
public class WorkflowController {

    @Autowired
    private WorkflowService workflowService;

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private IdentityService identityService;

    @Autowired
    private SysUserService userService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private RoleUserService roleUserService;

    @Autowired
    private ActAssigneeService actAssigneeService;

    @Autowired
    private TaskService taskService;


    @RequestMapping(value = "deploy", method = RequestMethod.GET)
    @RequiresPermissions("show:lcbs")
    public String toDeploy(org.springframework.ui.Model model) {
        return "/deploy";
    }

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public String toList() {
        return "workflow/process/shlist";
    }
    /*
    * @Author:zhaoyongke
    * @Description: 流程部署
    * @param null
    * @Date:17:37 2020/11/10
    */
    @RequestMapping(value = "/addDeploy", method = RequestMethod.POST)
    @ResponseBody
    public JsonUtil addDeploy(@RequestParam(value = "file", required = false) MultipartFile filedata, HttpServletRequest request) {
        JsonUtil re = new JsonUtil();
        try {
            Deployment  deployment=  workflowService.deploy(filedata.getOriginalFilename(), filedata.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
            re.setMsg("部署失败");
            re.setFlag(false);
            return  re;
        }
        re.setMsg("部署成功");
        re.setFlag(true);
        return  re;
    }

    /**
    * @Author:zhaoyongke
    * @Description: 完成任务
    * @param request
    * @Date:12:01 2020/11/14
    */
    @RequestMapping(value = "/finishTask", method = RequestMethod.GET)
    @ResponseBody
    public JsonUtil finishTask( HttpServletRequest request) {
        JsonUtil re = new JsonUtil();
/*
        Object   znum= runtimeService.getVariable("40020","nrOfInstances");
        Object   finshnum= runtimeService.getVariable("40020","nrOfCompletedInstances");
*/
        Map<String, Object> variables =new HashMap<String, Object>();
        variables.put("zjchek","-1");//审核通过
        variables.put("js","acfc0e9232f54732a5d9ffe9071bf572");// 下一任务审核人


        //runtimeService.setVariable("40028","nrOfCompletedInstances",4);;
        workflowService.completeTask("40176",false, variables);
        re.setMsg("部署成功");
        re.setFlag(true);
        return  re;
    }





}
